package com.example.service;

import com.example.entity.Hengxiang;
import com.example.mapper.HengxiangMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class HengxiangService {

    @Resource
    private HengxiangMapper hengxiangMapper;

    // 添加横向记录
    public String add(Hengxiang hengxiang) {
        try {
            hengxiangMapper.insert(hengxiang);
        } catch (DuplicateKeyException e) {
            return "重复！"; // 捕获主键重复异常，返回错误信息
        }
        return null; // 添加成功，返回null
    }

    // 根据ID更新横向记录
    public void updateById(Hengxiang hengxiang) {
        hengxiangMapper.updateById(hengxiang);
    }

    // 根据ID查询横向记录
    public Hengxiang getById(Integer id) {
        return hengxiangMapper.selectById(id);
    }

    // 根据ID删除横向记录
    public void deleteById(Integer id) {
        hengxiangMapper.deleteById(id);
    }

    // 分页查询横向记录列表
    public PageInfo<Hengxiang> selectPage(Integer pageNum, Integer pageSize, Hengxiang hengxiang) {
        PageHelper.startPage(pageNum, pageSize); // 设置分页信息
        List<Hengxiang> hengxiangs = hengxiangMapper.selectAll(hengxiang); // 查询横向记录列表
        return PageInfo.of(hengxiangs); // 返回分页信息
    }
}
